package route

import (
	"ezcloud/gw/internal/conf"
	"io"
	"net"
	"net/http"
)

type RouteFilterChain interface {
	Filter(context *RouteContext)
}

var X_ROUTE = "reqeust.route"

var X_FILTER_DEFINES = "reqeust.filterdefines"

type RouteContext struct {
	Tcc       *conf.TcpConnWrap
	LocalAddr net.Addr
	RemoteAdd net.Addr

	// 原始请求和原始近回
	OrigRequest  *http.Request
	OrigResponse http.ResponseWriter

	// x-request-id
	RequestId string

	// upstream request
	RequestMethod string
	RequestUri    string
	RequestHeader http.Header
	RequestBody   io.ReadCloser

	// upstream response
	ResponseCode   int
	ResponseHeader http.Header
	ResponseBody   io.ReadCloser

	// return result
	ReturnStatus int
	ReturnHeader http.Header
	ReturnBody   io.ReadCloser

	// 属性
	attributes map[string]interface{}
}

func NewGwRouteContext() *RouteContext {
	return &RouteContext{attributes: make(map[string]interface{}, 16)}
}

func (c *RouteContext) Get(k string) interface{} {
	return c.attributes[k]
}

func (c *RouteContext) Set(k string, v interface{}) {
	c.attributes[k] = v
}

func (c *RouteContext) Del(k string) {
	delete(c.attributes, k)
}

func (c *RouteContext) Has(k string) bool {
	_, ok := c.attributes[k]
	return ok
}

func (c *RouteContext) Destory() {
	c.Tcc = nil
}
